package org.pliers.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.pliers.feign.ServiceFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class ConfigInfoService {

    private static final Logger log = LoggerFactory.getLogger(ConfigInfoService.class);

    @Value("${config.info}")
    private String configInfo;

    @Autowired
    private ServiceFeign serviceFeign ;

    @SentinelResource(value = "configInfo",fallback = "fallbackHandler", blockHandler = "blockHandler" )
    public String  getConfigInfo(String id) throws Exception {
        if(id.equals("2")) throw new Exception();
        String retString = "default" ;
        try{
            retString = serviceFeign.getConfigInfo() ;
        }catch (Exception ex){
            log.error("error",ex);
        }
        return configInfo + id + retString ;
    }

    public String fallbackHandler(String id) {
        return "fallback"+id;
    }

    public String blockHandler (String id , BlockException e) {
        log.error("block error",e);
        return "service block:"+id;
    }
}
